﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Diagnostics;

using System.Collections.Generic;
using System.Runtime.InteropServices.Automation;
using System.IO;



namespace SearchClient
{
    public partial class Home : Page
    {
        private ServiceReference.WCFServiceClient proxy;
        private IList<string> sourcesVideos = new List<string>();
        int i = 0;

        public Home()
        {

            InitializeComponent();
            proxy = new ServiceReference.WCFServiceClient();
         
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            
            string topic = txtSearchTopic.Text;
            if (topic != null || topic != "")
            {
                StoriesList.SelectedIndex = -1;
                StoriesList.ItemsSource = topic;
                proxy.BuscarFileSystemElementsCompleted +=new EventHandler<ServiceReference.BuscarFileSystemElementsCompletedEventArgs>(proxy_BuscarFileSystemElementsCompleted);
                proxy.BuscarFileSystemElementsAsync(topic, UserHolder.UserName);
            }
        }

        void proxy_BuscarFileSystemElementsCompleted(object sender, ServiceReference.BuscarFileSystemElementsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Error conectando con el servicio. Reintente");
            }
            else
            {
                 IList<ServiceReference.FileSystemDTO> results = e.Result;
                 StoriesList.SelectedIndex = -1;
                 StoriesList.ItemsSource = results;
               
            }
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))

            {
              
                mediaElement1.AutoPlay = true;
                proxy.ObtenerVideosCompleted += new EventHandler<ServiceReference.ObtenerVideosCompletedEventArgs>(proxy_ObtenerVideosCompleted);
                proxy.ObtenerVideosAsync();
            }
           

        }

        void proxy_ObtenerVideosCompleted(object sender, ServiceReference.ObtenerVideosCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //textBlockErrors.Text = "Error conectando con el servicio. Reintente";
            }
            else
            {
                sourcesVideos = (IList<string>)e.Result;
                string file = sourcesVideos[i].Replace("\\","/");

                mediaElement1.Source = new Uri("file:" + file);
                i++;

            }
        }

        private void mediaElement1_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (i < sourcesVideos.Count)
            {
                string file = sourcesVideos[i].Replace("\\", "/");

                mediaElement1.Source = new Uri("file:" + file);
                i++;
            }
            else
            {
                i = 0;
                string file = sourcesVideos[i].Replace("\\", "/");

                mediaElement1.Source = new Uri("file:" + file);
            }
        }

            }
}